//
//  UIViewController+RBMediaAdd.m
//  RBDemoProj
//
//  Created by Mac on 2020/12/1.
//  Copyright © 2020 SummerTea. All rights reserved.
//

#import "UIViewController+RBMediaAdd.h"
#import <NSObject+YYAdd.h>

static char *const RB_FORCE_PORTRAIT_KEY = "RB_FORCE_PORTRAIT_KEY";

@implementation UIViewController(RBMediaAdd)

- (void)setForcePortrait:(BOOL)forcePortrait
{
    [self setAssociateValue:[NSNumber numberWithBool:forcePortrait] withKey:RB_FORCE_PORTRAIT_KEY];
}

- (BOOL)forcePortrait
{
    NSNumber *number = [self getAssociatedValueForKey:RB_FORCE_PORTRAIT_KEY];
    if (!number)
    {
        return YES;
    }
    else
    {
        return number.boolValue;
    }
}
    
+ (void)load
{
    [self swizzleInstanceMethod:@selector(shouldAutorotate) with:@selector(shouldAutorotateSwizzled)];
    [self swizzleInstanceMethod:@selector(supportedInterfaceOrientations) with:@selector(supportedInterfaceOrientationsSwizzled)];
    [self swizzleInstanceMethod:@selector(preferredInterfaceOrientationForPresentation) with:@selector(preferredInterfaceOrientationForPresentationSwizzled)];
}

-(BOOL)shouldAutorotateSwizzled
{
    if ([self isKindOfClass:UINavigationController.class])
    {
        UINavigationController *naviVc = (UINavigationController *)self;
        return [naviVc.viewControllers.lastObject shouldAutorotate];
    }
    else if([self isKindOfClass:UITabBarController.class])
    {
        UITabBarController *tabVc = (UITabBarController *)self;
        return [tabVc.selectedViewController shouldAutorotate];
    }
    else
    {
        if (self.forcePortrait)
        {
            return NO;
        }
        else
        {
            return [self shouldAutorotateSwizzled];
        }
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientationsSwizzled
{
    if ([self isKindOfClass:UINavigationController.class])
    {
        UINavigationController *naviVc = (UINavigationController *)self;
        return [naviVc.viewControllers.lastObject supportedInterfaceOrientations];
    }
    else if([self isKindOfClass:UITabBarController.class])
    {
        UITabBarController *tabVc = (UITabBarController *)self;
        return [tabVc.selectedViewController supportedInterfaceOrientations];
    }
    else
    {
        if (self.forcePortrait)
        {
            return UIInterfaceOrientationMaskPortrait;
        }
        else
        {
            return [self supportedInterfaceOrientationsSwizzled];
        }
    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentationSwizzled
{
    if ([self isKindOfClass:UINavigationController.class])
    {
        UINavigationController *naviVc = (UINavigationController *)self;
        return [naviVc.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
    }
    else if([self isKindOfClass:UITabBarController.class])
    {
        UITabBarController *tabVc = (UITabBarController *)self;
        return [tabVc.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    else
    {
        if (self.forcePortrait)
        {
            return UIInterfaceOrientationPortrait;
        }
        else
        {
            return [self preferredInterfaceOrientationForPresentationSwizzled];
        }
    }
}

@end
